from django.db import models

# Create your models here.
from django.db import models
from utils.models import BaseModel


class GoodsCategory(BaseModel):
    "商品列表"
    # 商品名称
    name = models.CharField(max_length=10, verbose_name='名称')
    # 父类别
    parent = models.ForeignKey('self', null=True, blank=True,
                               on_delete=models.CASCADE,
                               verbose_name='父类别')

    class Meta:
        db_table = 'tb_goods_category'
        verbose_name = '商品类别'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class GoodsChannel(BaseModel):
    # 商品频道
    # 所属组
    group_id = models.IntegerField(verbose_name='组号')
    # 类别名称
    category = models.ForeignKey(GoodsCategory, on_delete=models.CASCADE, verbose_name='顶级商品类别')
    # 频道页链接地址
    url = models.CharField(max_length=50, verbose_name='频道页面链接')
    # 序号
    sequence = models.IntegerField(verbose_name='组内顺序')

    class Meta:
        db_table = 'tb_goods_channel'
        verbose_name = '商品频道'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.category.name


class Brand(BaseModel):
    """
    品牌
    """
    name = models.CharField(max_length=20, verbose_name='名称')
    # ｌｏｇｏ图片链接
    logo = models.ImageField(verbose_name='Logo图片')
    # 品牌首字母
    first_letter = models.CharField(max_length=1, verbose_name='品牌首字母')

    class Meta:
        db_table = 'tb_brand'
        verbose_name = '品牌'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Goods(BaseModel):
    """
    商品SPU
    """
    name = models.CharField(max_length=50, verbose_name='名称')
    # 品牌名称
    brand = models.ForeignKey(Brand, on_delete=models.PROTECT, verbose_name='品牌')
    # 一级类别
    category1 = models.ForeignKey(GoodsCategory, on_delete=models.PROTECT, related_name='cat1_goods',
                                  verbose_name='一级类别')
    # 二级类别
    category2 = models.ForeignKey(GoodsCategory, on_delete=models.PROTECT, related_name='cat2_goods',
                                  verbose_name='二级类别')
    # 三级类别
    category3 = models.ForeignKey(GoodsCategory, on_delete=models.PROTECT, related_name='cat3_goods',
                                  verbose_name='三级类别')
    # 销量
    sales = models.IntegerField(default=0, verbose_name='销量')
    # 评价量
    comments = models.IntegerField(default=0, verbose_name='评价数')

    class Meta:
        db_table = 'tb_goods'
        verbose_name = '商品'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class GoodsSpecification(BaseModel):
    """
    商品规格
    """
    # 商品
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name='商品')
    # 商品名称
    name = models.CharField(max_length=20, verbose_name='规格名称')

    class Meta:
        db_table = 'tb_goods_specification'
        verbose_name = '商品规格'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s: %s' % (self.goods.name, self.name)


class SpecificationOption(BaseModel):
    """
    规格选项
    """
    # 规格
    spec = models.ForeignKey(GoodsSpecification, on_delete=models.CASCADE, verbose_name='规格')
    # 规格具体值
    value = models.CharField(max_length=20, verbose_name='选项值')

    class Meta:
        db_table = 'tb_specification_option'
        verbose_name = '规格选项'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s - %s' % (self.spec, self.value)


class SKU(BaseModel):
    """
    商品SKU
    """
    name = models.CharField(max_length=50, verbose_name='名称')
    # 副标题
    caption = models.CharField(max_length=100, verbose_name='副标题')
    # 商品
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name='商品')
    # 分类
    category = models.ForeignKey(GoodsCategory, on_delete=models.PROTECT, verbose_name='从属类别')
    # 价格
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='单价')
    # 进价
    cost_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='进价')
    # 市场售价
    market_price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='市场价')
    # 库存
    stock = models.IntegerField(default=0, verbose_name='库存')
    # 销量
    sales = models.IntegerField(default=0, verbose_name='销量')
    # 评价量
    comments = models.IntegerField(default=0, verbose_name='评价数')
    # 是否上架
    is_launched = models.BooleanField(default=True, verbose_name='是否上架销售')
    # 默认图片地址
    default_image_url = models.CharField(max_length=200, default='', null=True, blank=True, verbose_name='默认图片')

    class Meta:
        db_table = 'tb_sku'
        verbose_name = '商品SKU'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s: %s' % (self.id, self.name)


class SKUSpecification(BaseModel):
    """
    SKU具体规格
    """
    # 所属
    sku = models.ForeignKey(SKU, on_delete=models.CASCADE, verbose_name='sku')
    # 规格
    spec = models.ForeignKey(GoodsSpecification, on_delete=models.PROTECT, verbose_name='规格名称')
    # 规格选项
    option = models.ForeignKey(SpecificationOption, on_delete=models.PROTECT, verbose_name='规格值')

    class Meta:
        db_table = 'tb_sku_specification'
        verbose_name = 'SKU规格'
        verbose_name_plural = verbose_name

    def __str__(self):
        return '%s: %s - %s' % (self.sku, self.spec.name, self.option.value)


class ContentCategory(BaseModel):
    """
    广告内容类别
    """
    name = models.CharField(max_length=50, verbose_name='名称')
    # 类别识别键名
    key = models.CharField(max_length=50, verbose_name='类别键名')

    class Meta:
        db_table = 'tb_content_category'
        verbose_name = '广告内容类别'
        verbose_name_plural = verbose_name


class Content(BaseModel):
    """
    广告内容
    """
    # 广告列表，on_delete=models.PROTECT，外键守护模式，不让删除
    category = models.ForeignKey(ContentCategory, on_delete=models.PROTECT, verbose_name='类别')
    # 广告标题
    title = models.CharField(max_length=100, verbose_name='标题')
    # 内容链接地址
    url = models.CharField(max_length=300, verbose_name='内容链接')
    # 广告图片地址
    image = models.ImageField(null=True, blank=True, verbose_name='图片')
    # ／内容
    text = models.TextField(null=True, blank=True, verbose_name='内容')
    # 排序
    sequence = models.IntegerField(verbose_name='排序')

    status = models.BooleanField(default=True, verbose_name='是否展示')

    class Meta:
        db_table = 'tb_content'
        verbose_name = '广告内容'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.category.name + ': ' + self.title
